<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWeProduct
 *
 * @author i062261
 */
class BWeProduct extends BApp {

    //put your code here

    public function run($data = NULL) {
        parent::run($data);
        //$cooker = new BAEventCooker();
        $eventCooker= new BAEventCooker();
        $eventCooker->addEventType('weProduct', '微产品');
        $eventCooker->addEventType('weOrder', '微订单');
        $eventCooker->addEventType('weBuyer', '微买家');
        $eventCooker->addEventType('weVender', '微卖家');           

        //register child control
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $appAdminControl = new BUIAppAdmin('appAdmin');
        $screen->addChildControl($appAdminControl);

        //register role control
        $roleControl = new BUIRole('roleControl');
        $screen->addChildControl($roleControl);
        
        //register attribute control
        $attributeControl = new BUIAttribute('attributeControl');
        $screen->addChildControl($attributeControl);
        
        //register category control
        $categoryControl = new BUICategory('categoryControl');
        $screen->addChildControl($categoryControl);

        $sender = Blyn::app()->getUIManager()->getSenderControl();

        if ($sender instanceof BUIOperation) {
            $screen = Blyn::app()->getUIManager()->getCurrentScreen();
            $eventCooker->onShowEventList($screen);
        }
    }

    public function addEventType() {
        $category = new BPCategory();
        $rootNode = $category->getRootNode();
        //add event type

        $setupProductType = $rootNode->getChild('eventType.admin.setupProduct');

        if (!$setupProductType instanceof BPCategory) {
            $rootNode->addChild('eventType.admin.setupProduct');
        }

        $setupBuyerType = $rootNode->getChild('eventType.admin.setupBuyer');

        if (!$setupBuyerType instanceof BPCategory) {
            $rootNode->addChild('eventType.admin.setupBuyer');
        }

        $setupVenderType = $rootNode->getChild('eventType.admin.setupVender');

        if (!$setupVenderType instanceof BPCategory) {
            $rootNode->addChild('eventType.admin.setupVender');
        }

        $setupOnlineOrderType = $rootNode->getChild('eventType.admin.setupOnlineOrder');

        if (!$setupOnlineOrderType instanceof BPCategory) {
            $rootNode->addChild('eventType.admin.setupOnlineOrder');
        }

        $clientOrderProductType = $rootNode->getChild('eventType.client.orderProduct');

        if (!$clientOrderProductType instanceof BPCategory) {
            $rootNode->addChild('eventType.client.orderProduct');
        }
    }

    /**
     * 
     * @param type $data, if data = 'admin', will return all event for admin operation
     * @return type
     */
    public function getEventList($data = NULL) {

        if ($data == NULL) {
            $data = Blyn::app()->getActiveOperation()->getName();
        }

        $typeList = $this->getEventTypeList($data);

        $list = array();
        foreach ($typeList as $id => $typeName) {
            $type = new BPCategory($id);
            $events = $type->getMembers(new BTreeList());
            $list += $events;
        }
        //$eventTable = new BPEventSheet();
        //$eventList = $eventTable->getView();

        return $list;
    }

    /**
     * 
     * @param type $data, null or string, if null, return all types, if string
     * @return type
     */
    public function getEventTypeList($data = NULL) {
        $eventCooker = new BAEventCooker();
        return $eventCooker->getEventTypeList('alias');
        /*
          if ($data == NULL) {
          $data = Blyn::app()->getActiveOperation()->getName();
          }
          $category = new BPCategory();
          //$category->setOwner(Blyn::app()->getActiveApp());
          $parentEventType = 'eventType';
          if (is_string($data) && $data != NULL) {
          $parentEventType = 'eventType.' . $data;
          }
          $eventTypeNode = $category->getRootNode()->getChild($parentEventType);
          $eventTypeFullName = $eventTypeNode->getFullName();
          $children = $eventTypeNode->getChildren();
          $list = array();
          foreach ($children as $id => $typeNode) {
          $fullName = $typeNode->getFullName();
          $name = substr($fullName, strlen($eventTypeFullName) + 1);
          if ($name != 'setupProduct')
          $list[$id] = $name;
          }
          return $list;
         * 
         */
    }

    public function getEventTypeName($event) {
        $category = new BPCategory();
        $eventTypeNode = $category->getRootNode()->getChild('eventType');
        $eventTypeFullName = $eventTypeNode->getFullName();
        if ($event != NULL) {
            $eventType = BPCategory::getMemberCategory($event);
            $typeList = $this->getEventTypeList();
            $eventTypeId = $eventType->getId();
            return $typeList[$eventTypeId];
        }
    }

    public function getEventType($event) {
        if ($event != NULL) {
            $eventType = BPCategory::getMemberCategory($event);
            return $eventType;
        }
    }

    public function getPriceTypeList($data = NULL) {
        return array(
            'priceType.priceByQuotation' => '通过报价单报价',
            'priceType.priceByOnline' => '使用在线价格',
        );
    }

    public function getPriceChangeUnitList($data = NULL) {

        $list = array();
        $list['percent'] = '百分比％';
        $list['cn-yuan'] = '人民币元';
        $list['sg-dollar'] = '新加坡元';
        $list['us-dollar'] = '美元';
        $list['eu-euro'] = '欧元';
        return $list;
    }

    public function getRegionList($country = NULL) {

        /*
          $addressTable = new BPSheet('address');

          $sg = $addressTable->getList(array('itemData' => array(
          'name' => 'singapore.singapore'
          )));

          if ($sg == NULL) {
          $addressTable->addList(array('itemData' => array(
          'name' => 'singapore.singapore'
          )));
          }

          $view = $addressTable->getView();
         * 
         */




        $root = BWAddress::getRoot();

        if (!$root->getChild('singapore.singapore') instanceof BAttribute) {
            $root->addAttribute('singapore.singapore', 'Singapore');
        }

        if (!$root->getChild('china.chongqing') instanceof BAttribute) {
            $root->addAttribute('china.chongqing', '重庆');
        }

        $children = $root->getChildrenAsLeaf();

        $list = array();
        //return name and alias
        if (is_array($children)) {
            foreach ($children as $id => $address) {
                $list[$address->getFullName()] = $address->value;
            }
        }
        return $list;
    }

    public function getDataTypeList($data = NULL) {
        $typeList = array(
            'string' => 'String',
            'text' => 'Text',
            'number' => 'Number',
            'datetime' => 'DateTime',
            'image' => 'Image'
        );
        return $typeList;
    }

    public function showActiveFieldByDataType($dataType = 'string', $modelData = array()) {

        if ($dataType == NULL) {
            $dataType = 'string';
        }

        $model = NULL;
        $form = NULL;
        $field = NULL;
        $data = NULL;
        $htmlOptions = array();

        if (isset($modelData['model']))
            $model = $modelData['model'];

        if (isset($modelData['field']))
            $field = $modelData['field'];

        if (isset($modelData['data']))
            $data = $modelData['data'];

        if (isset($modelData['htmlOptions']))
            $htmlOptions = $modelData['htmlOptions'];

        $dataType = strtolower($dataType);

        if (strtolower($dataType) == 'string' || strtolower($dataType) == 'number') {
            if ($form != NULL) {
                return $form->activeTextField($model, $field, $htmlOptions);
            } else {
                return CHtml::activeTextField($model, $field, $htmlOptions);
            }
        }

        if ($dataType == 'text') {
            if ($form != NULL) {
                return $form->activeTextArea($model, $field, $htmlOptions);
            } else {
                return CHtml::activeTextArea($model, $field, $htmlOptions);
            }
        }

        if ($dataType == 'array') {
            if ($data == NULL)
                $data = array();
            if ($form != NULL) {
                return $form->activeTextField($model, $field, $htmlOptions);
            } else {
                return CHtml::activeDropDownList($model, $field, $data, $htmlOptions);
            }
        }

        if ($dataType == 'datetime') {
            $htmlOptions = array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '时间...');
            $str = '';
            $str .='<div class="input-append" id="datetimepicker1">';
            if ($data == NULL)
                $data = array();
            if ($form != NULL) {
                $str .=$form->activeTextField($model, $field, $htmlOptions);
                $str .='<span class="add-on">
                            <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                            </i>
                        </span>
                    </div> ';
                return $str;
            } else {
                $str .=CHtml::activeTextField($model, $field, $htmlOptions);
                $str .='<span class="add-on">
                            <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                            </i>
                        </span>
                    </div> ';
                return $str;
            }
        }

        if ($dataType == 'image') {
            $oName = explode("_", $field);
            $field = 'media_image_' . $oName[2] . '_' . $oName[3];
//            $field = 'image_media_image_attribute';
            $str = '';
            //$str = '<div class="row-fluid"><div class="span6"><nobr style="color: red;font-size:20px">*</nobr>' . 'image' . '</div>';
//            $str .= '<div class="span6">' . CHtml::activeFileField($model, $field);
            $str .= '<div>' . CHtml::activeFileField($model, $field);
            if ($model->$field != NULL) {
                $imageUrl = Yii::app()->request->baseUrl . $model->$field;
                $str.= "<img src=" . $imageUrl . " width=50 height=50 />";
            }

            //$str .= '</div></div>';
            $str .='</div>';

            return $str;
        }
    }

    public function getModelDataByDataType($dataTypeItem) {

        if ($dataTypeItem instanceof BTreeItem) {
            $type = $dataTypeItem->getValue();
            $dataTypeTable = new BPSheet('dataTypeData');

            if ($type == 'array') {
                $list = array();
                $dataTypeView = $dataTypeTable->getViewByOwner($dataTypeItem);
                foreach ($dataTypeView as $list) {
                    $keyItem = $list->getItem('key');
                    if ($keyItem instanceof BTreeItem)
                        $key = $keyItem->getValue();

                    $valueItem = $list->getItem('value');
                    if ($valueItem instanceof BTreeItem)
                        $value = $valueItem->getValue();

                    $list[$key] = $value;
                    return $list;
                }
            }
        }
    }

    public function getCustomerRoles($data = NULL) {
        $roles = parent::getRoles(array('type' => BRole::Role_App_customer), TRUE);
        $list = array();
        foreach ($roles as $id => $role) {
            $list[$id] = $role->getName();
        }
        return $list;
    }

    public function populateOrders($buyerData = NULL, $timeData = NULL, $productData = NULL, $data = NULL) {

        $productTable = new BPProductSheet();
        $productView = $availableOrder->getView(array('itemData' => $itemData));
        $orderList = array();

        foreach ($productView as $productList) {
            $productId = $productList->getItem('productId')->getValue();
            $productPrice = $this->populateProductPrice($productId);
            if ($productPrice) {
                $orderList['productId'] = $productList;
            }
        }
        return $orderList;
    }

    /**
     * this function first find order config for user and timeslot
     * then find productId or productCategory in orderConfig
     * then return all product with productId and product category
     * @param type $productData
     * @param type $buyerData
     * @param type $timeData
     * @param type $data
     * @return type
     */
    public function populateAvailableProductView($productData = NULL, $buyerData = NULL, $timeData = NULL, $data = NULL) {
        $buyerMemberList = $this->populateBuyerMemberList($buyerData);
        $timeSlotList = $this->populateTimeSlotList($timeData);

        $itemData = array();
        $buyerCategoryItemData = array();
        $buyerIdItemData = array();

        if ($buyerCategoryList != NULL) {
            foreach ($buyerCategoryList as $list) {
                $buyerCategoryItem = $list->getItem('buyerCategory');
                if ($buyerCategoryItem) {
                    $buyerCategory = $buyerCategoryItem->getValue();
                    $buyerCategoryItemData[$buyerCategory] = $buyerCategory;
                }
                $buyerIdItem = $list->getItem('buyerId');
                if ($buyerIdItem) {
                    $buyerId = $buyerIdItem->getValue();
                    $buyerIdItemData[$buyerId] = $buyerId;
                }
            }
        }

        if ($timeSlotList != NULL) {
            $itemData['timeSlotId'] = array();
            foreach ($timeSlotList as $list) {
                $timeSlotId = $list->getItem('timeSlotId')->getValue();
                $itemData['timeSlotId'][$timeSlotId] = $timeSlotId;
            }
        }

        $orderConfigTable = new BPOrderSheet('orderConfig');

        $orderConfigView = array();
        if ($buyerCategoryItemData != NULL) {
            $itemData['buyerCategory'] = $buyerCategoryItemData;
            $orderConfigView += $orderConfigTable->getView(array('itemData' => $itemData));
            unset($itemData['buyerCategory']);
        }
        if ($buyerIdItemData != NULL) {
            $itemData['buyerId'] = $buyerIdItemData;
            $orderConfigView += $orderConfigTable->getView(array('itemData' => $itemData));
            unset($itemData['buyerId']);
        }

        $productTable = new BPProductSheet();
        $productView = $productTable->getView();
        $productIdList = array();
        $productCategoryList = array();

        foreach ($orderConfigView as $orderConfig) {
            $productIdItem = $orderConfig->getItem('productId');
            $productCategoryItem = $orderConfig->getItem('productCategory');

            //if not set productCategory or product, means apply to all product
            if ($productIdItem == NULL && $productCategoryItem == NULL) {
                return $productView;
            } else {//otherwise , populate category and productId
                if ($productIdItem) {
                    $productId = $productIdItem->getValue();
                    $productIdList[$productId] = $productId;
                }
                if ($productCategoryItem) {
                    $productCategory = $productCategoryItem->getValue();
                    $productCategoryList[$productCategory] = $productCategory;
                }
            }
        }

        $itemData = array();
        $productCategoryItemData = array();
        $productIdItemData = array();
        foreach ($productCategoryList as $productCategory) {
            $itemData['productCategory'] = array();
            foreach ($productCategoryList as $list) {
                $productCategory = $list->getItem('productCategory')->getValue();
                $productCategoryItemData[$productCategory] = $productCategory;
                //$itemData['productCategory'][$productCategory] = $productCategory;
            }
        }

        foreach ($productIdList as $productId) {
            $itemData['productId'] = array();
            foreach ($productIdList as $list) {
                $productId = $list->getItem('productId')->getValue();
                $productIdItemData[$productId] = $productId;
                //$itemData['productId'][$productId] = $productId;
            }
        }

        $productCategoryTable = new BPSheet('productCategory');
        $productList = array();
        //this function will 
        foreach ($productView as $productId => $product) {
            if (in_array($productId, $productIdList))
                continue;
            else {
                $itemData = array();
                $itemData['productId'] = $productId;
                $itemData['productCategory'] = $productCategoryItemData;
                $productCategoryList = $productCategory->getList(array('itemData' => $itemData));
                if ($productCategoryList)
                    $productIdListList[$productId] = $productId;
            }
        }
        return $productList;
    }

    public function populateBuyerMemberList($buyerData = NULL) {
        if ($buyerData == NULL) {
            $user = Blyn::app()->getCurrentUser();
        }
        $userRoles = BRole::findUserRoles(NULL, NULL, TRUE);

        $buyerMemberTable = new BPSheet('buyerMember');
        $itemData = array();
        $itemData['userId'] = $user->getId();
        $itemData['roleId'] = array();
        foreach ($userRoles as $roleId => $role) {
            $itemData['roleId'][$roleId] = $roleId;
        }
        $buyerMemberView = $buyerMemberTable->getView($itemData);
        return $buyerMemberView;
    }

    public function populateProductCategoryList($productId) {

        $productCategoryTable = new BPSheet('productCategoryMember');
        $categoryView = $productCategoryTable->getView(array('itemData' => array('productId' => $productId)));

        return $categoryView;
    }

    public function populateTimeSlotList($timeSlotData = NULL) {
        return array();
    }

    public function populateProductPrice($productId, $buyerData = NULL, $timeSlotData = NULL) {

        $productTable = new BPProductSheet();
        $productList = $productTable->getListById($productId);
        $productPriceItem = $productList->getItem('price');

        if ($productPriceItem) {
            $productPrice = $productPriceItem->getValue();
            $minPrice = $productPrice;
            $maxPrice = $productPrice;
            $lastPrice = $productPrice;

            $buyerCategoryList = $this->populateBuyerCategoryList($buyerData);
            $productCategoryList = $this->populateProductCategoryList($productData);
            $timeSlotList = $this->populateTimeSlotList($timeSlotData);

            $discountTable = new BPOrderSheet('discount');
            $discountView = array();

            $itemData = array();

            if ($buyerCategoryList != NULL) {
                //support search by 'or' condition
                $itemData['buyerCategory'] = array();
                foreach ($buyerCategoryList as $list) {
                    $buyerCategory = $list->getItem('buyerCategory')->getValue();
                    array_push($itemData['buyerCategory'], $buyerCategory);
                }
            }

            if ($timeSlotList != NULL) {
                $itemData['timeSlotId'] = array();
                foreach ($timeSlotList as $list) {
                    $timeSlotId = $list->getItem('timeSlotId')->getValue();
                    array_push($itemData['timeSlotId'], $timeSlotId);
                }
            }

            $discountView = $discountTable->getView(array('itemData' => $itemData), FALSE);

            foreach ($discountView as $discountList) {
                $productPriceItem = $discountList->getItem('productPrice');
                if ($productPriceItem) {
                    $priceChangeWay = $discountList->getItem('priceChangeWay')->getValue();
                    $priceChangeAmount = $discountList->getItem('priceChangeAmount')->getValue();
                    $priceChangeUnit = $discountList->getItem('priceChangeUnit')->getValue();


                    if ($priceChangeWay == 'addPrice') {
                        if ($priceChangeUnit == 'percent') {
                            $productPrice = $productPrice + $productPrice * $priceChangeAmount / 100;
                        } else {
                            $productPrice = $productPrice + $priceChangeAmount;
                        }
                        if ($maxPrice < $productPrice) {
                            $maxPrice = $productPrice;
                        }
                        $lastPrice = $productPrice;
                    }
                    if ($priceChangeWay == 'discount') {
                        if ($priceChangeUnit == 'percent') {
                            $productPrice = $productPrice - $productPrice * $priceChangeAmount / 100;
                        } else {
                            $productPrice = $productPrice - $priceChangeAmount;
                        }
                        if ($minPrice > $productPrice) {
                            $minPrice = $productPrice;
                        }
                        $lastPrice = $productPrice;
                    }
                }
            }

            return array('minPrice' => $minPrice, 'maxPrice' => $maxPrice, $lastPrice => $lastPrice);
        }
        return FALSE;
    }

    public function createEvent($categoryId, $eventName) {
        $eventTable = new BPEventSheet();
        $event = $eventTable->getNewList();
        $event->addItem("name", $eventName);
        $event->addItem("description", "");

        if (isset($categoryId) && $categoryId > 0) {
            $category = new BPCategory($categoryId);
            $category->addMember($event);
            $event->addItem('eventType', $category->getFullName());
            $event->addItem('eventTypeId', $categoryId);
        }
    }

    public function runAdmin($data = NULL) {
        
        //$eventCooker= new BAEventCooker();
        //register initial events
        /*
        $productEvent = $eventCooker->getEvent('productEvent', 'changeless.weProduct');
        if ($productEvent == FALSE) {
            $eventCooker->addEvent('productEvent', 'changeless.weProduct');
        }
        $buyerEvent = $eventCooker->getEvent('buyerEvent', 'changeless.weBuyer');
        if ($buyerEvent == FALSE) {
            $eventCooker->addEvent('buyerEvent', 'changeless.weBuyer');
        }
        $sellerEvent = $eventCooker->getEvent('sellerEvent', 'changeless.weSeller');
        if ($sellerEvent == FALSE) {
            $eventCooker->addEvent('sellerEvent', 'changeless.weSeller');
        }
        $orderEvent = $eventCooker->getEvent('orderEvent', 'changeless.weOrder');
        if ($orderEvent == FALSE) {
            $eventCooker->addEvent('orderEvent', 'changeless.weOrder');
        }
         * 
         */

        /*
          $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
          $senderControl = Blyn::app()->getUIManager()->getSenderControl();
          $screen = Blyn::app()->getUIManager()->getCurrentScreen();
          $screen->addChildControl(new BUIRole('roleControl'));
          $app = Blyn::app()->getActiveApp();
          $processPostObj = new BAProcessData();
          $postData = $processPostObj->parsedModelData();

          if ($senderControl instanceof BUIOperation) {
          $eventCooker = new BAEventCooker();
          $eventCooker->showEventListByOperation(Blyn::app()->getUIManager()->getCurrentScreen());
          }
         * 
         */


        /*
          if ($senderControl->isAjaxLinkClicked('deleteEvent')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          if ($eventId != NULL) {
          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);
          $event->getListNode()->status = -1;
          $event->save();
          }

          $updateControl->addRenderView('eventList', 'event_list', array('eventList' => $this->getEventList()));
          }

          if ($senderControl->isAjaxLinkClicked('deleteSelectedEvent')) {
          if (!empty($_POST)) {
          $checkedArray = $_POST['checkbox'];

          foreach ($checkedArray as $eventId) {
          if ($eventId != NULL) {
          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);
          $event->getListNode()->status = -1;
          $event->save();
          }
          }
          }

          $updateControl->addRenderView('eventList', 'event_list', array('eventList' => $this->getEventList()));
          }

          if ($senderControl->isAjaxLinkClicked('showManageEvent')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);
          $eventTypeId = $event->getItem('eventTypeId')->getValue();
          $eventType = new BPCategory($eventTypeId);
          $showView = NULL;

          if ($eventType->getName() == 'setupProduct') {
          $showView = 'product_list_1';
          }
          if ($eventType->getName() == 'setupBuyer') {
          $showView = 'buyer_list';
          }
          if ($eventType->getName() == 'setupVender') {
          $showView = 'vender_list';
          }
          if ($eventType->getName() == 'setupOnlineOrder') {
          $showView = 'orderConfig_list';
          }
          if ($showView != NULL)
          $updateControl->addRenderView('manageEvent', $showView, array('eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('showAddProduct')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $productId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('productId');

          $eventTable = new BPEventSheet();
          if (is_numeric($eventId) && $eventId > 0) {
          $event = $eventTable->getListById($eventId);

          if ($productId == NULL || $productId == 0) {
          $productTable = new BPProductSheet('product', Blyn::app()->getActiveSpace());
          $productName = 'product_' . Blyn::app()->getActiveSpace()->getId() . "_" . Blyn::app()->getCurrentUser()->getId();
          $productList = $productTable->getListByName($productName);
          if (!$productList instanceof BTreeList)
          $productList = $productTable->getNewList($productName);
          $productId = $productList->getId();
          }
          }
          $updateControl->addRenderView('showAddProduct', 'product_add', array(
          'venderId' => $venderId,
          'productId' => $productId,
          'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('deleteProduct')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $productId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('productId');

          $venderTable = new BPSheet('vender');
          $vender = $venderTable->getListById($venderId);
          $productTable = new BPProductSheet('product', $vender);
          if ($productId != NULL && $productId != 0) {
          $product = $productTable->deleteList($productId);
          }

          $updateControl->addRenderView('showProductList', 'product_list', array(
          'eventId' => $eventId,
          'venderId' => $venderId));
          }

          if ($senderControl->isAjaxLinkClicked('showProductList')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $updateControl->addRenderView('showProductList', 'product_list', array(
          'eventId' => $eventId,
          'venderId' => $venderId));
          }

          if ($senderControl->isAjaxLinkClicked('addProductCategory')) {
          $productId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('productId');
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');


          $productTable = new BPProductSheet('product');
          $product = $productTable->getListById($productId);

          $productCategoryTable = new BPCategorySheet('productCategory', $product);

          if ($categoryId == NULL || $categoryId == 0) {
          $category = new BPCategory();
          $productCategory = $productCategoryTable->getNewList();
          }

          if (is_numeric($categoryId) && $categoryId > 0) {
          $category = new BPCategory($categoryId);
          $productCategory = $productCategoryTable->getList(array('itemData' => array('productId' => $productId, 'categoryId' => $categoryId)));
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'product_category');

          if ($mediaList != NULL) {
          foreach ($mediaList as $media) {
          if ($media->name == 'name') {
          $categoryName = $media->content;
          }
          if ($media->name == 'alias') {
          $categoryValue = $media->content;
          }
          }

          $category = $category->getRootNode()->addChild(array('name' => $categoryName, 'value' => $categoryValue));
          $categoryId = $category->getId();
          }

          $productCategory->addItem('productId', $productId);
          $productCategory->addItem('categoryId', $categoryId);

          $updateControl->addRenderView('showAddProductCategory', 'product_add_category', array('productId' => $productId));
          }

          if ($senderControl->isAjaxLinkClicked('addProductAttribute')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $productId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('productId');
          $attributeId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('attributeId');


          $productTable = new BPProductSheet('product');
          $product = $productTable->getListById($productId);

          $attributeTable = new BPProductSheet('productAttribute', $product);

          if ($attributeId == NULL || $attributeId == 0) {
          $attribute = $attributeTable->getNewList();
          }

          if (is_numeric($attribute) && $attributeId > 0) {
          $attribute = $attributeTable->getListById($attributeId);
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'product_attribute');

          $items = array();
          foreach ($mediaList as $media) {
          $attribute->addItem($media->name, $media->content);
          }

          $updateControl->addRenderView('showAddProductAttribute', 'product_add_info', array('productId' => $productId, 'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('product_add_info_refresh')) {
          $productId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('productId');
          $updateControl->addRenderView('showProductInfo', 'product_add_info', array(
          'productId' => $productId,
          ));
          }

          if ($senderControl->isAjaxLinkClicked('saveProduct')) {
          $productId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('productId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');

          if (is_numeric($venderId) && $venderId > 0) {
          $venderTable = new BPSheet('vender');
          $vender = $venderTable->getListById($venderId);
          }

          $productTable = new BPProductSheet('product', $vender);

          if (is_numeric($productId) && $productId > 0) {
          $productList = $productTable->getListById($productId);
          $productList->setOwner($vender);
          } else {
          $productList = $productTable->getNewList();
          }

          //adjust list name
          $productListName = 'product_' . $productList->getId();
          $productList->name = $productListName;
          $productList->save();

          $postData = $processPostObj->parsedModelData();
          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'product_info');

          foreach ($mediaList as $media) {
          if ($media instanceof BWMedia) {
          $name = $media->getName();
          $value = $media->getContent();
          $productList->addItem($name, $value);
          }
          }

          //$product = $productTable->addList($productList, NULL, $event);

          $updateControl->addRenderView('productList', 'product_list', array('eventId' => $eventId, 'venderId' => $venderId));
          }

          if ($senderControl->isAjaxLinkClicked('showBuyerCategoryList')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $updateControl->addRenderView('showBuyerCategoryList', 'buyer_list', array('eventId' => $eventId));
          }
          //买家活动分类
          if ($senderControl->isLinkClicked('addEventCategory')) {
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          if (is_numeric($eventId) && $eventId > 0) {
          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);
          }

          $categoryTable = new BPSheet('buyerCategory', $event);
          if (is_numeric($categoryId) && $categoryId > 0) {
          $categoryList = $categoryTable->getListById($categoryId);
          $categoryList->setOwner($event);
          } else {
          $categoryList = $categoryTable->getNewList();
          }
          //adjust list name
          $categoryListName = 'buyerCategory_' . $categoryList->getId();
          $categoryList->name = $categoryListName;
          $categoryList->save();

          $postData = $processPostObj->parsedModelData();
          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'buyer_info');

          foreach ($mediaList as $media) {
          if ($media instanceof BWMedia) {
          $name = $media->getName();
          $value = $media->getContent();
          $categoryList->addItem($name, $value);
          }
          }

          $updateControl->addRenderView('buyerCategoryList', 'buyer_list', array('eventId' => $eventId));
          }


          if ($senderControl->isAjaxLinkClicked('showAddBuyerCategory')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');

          $eventTable = new BPEventSheet();
          if (is_numeric($eventId) && $eventId > 0) {
          $event = $eventTable->getListById($eventId);

          if ($categoryId == NULL || $categoryId == 0) {
          $buyerCategoryTable = new BPSheet('buyerCategory');
          $categoryName = 'buyerCategory_' . Blyn::app()->getActiveSpace()->getId() . "_" . Blyn::app()->getCurrentUser()->getId();
          $categoryList = $buyerCategoryTable->getListByName($categoryName);
          if (!$categoryList instanceof BTreeList)
          $categoryList = $buyerCategoryTable->getNewList($categoryName);
          $categoryId = $categoryList->getId();
          }
          }
          $updateControl->addRenderView('showAddBuyerCategory', 'buyer_add', array('categoryId' => $categoryId, 'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('addBuyerCategoryAttribute')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');
          $attributeId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('attributeId');


          $buyerCategoryTable = new BPSheet('buyerCategory');
          $category = $buyerCategoryTable->getListById($categoryId);

          $attributeTable = new BPSheet('buyerCategoryAttribute', $category);

          if ($attributeId == NULL || $attributeId == 0) {
          $attribute = $attributeTable->getNewList();
          }

          if (is_numeric($attribute) && $attributeId > 0) {
          $attribute = $attributeTable->getListById($attributeId);
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'buyerCategory_attribute');

          $items = array();
          foreach ($mediaList as $media) {
          $attribute->addItem($media->name, $media->content);
          }

          $updateControl->addRenderView('showAddBuyerCategory', 'buyer_add_info', array('categoryId' => $categoryId, 'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('manageBuyerCategoryMember')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');
          $memberId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryMemberId');

          if ($memberId == 'newMember') {
          $buyerCategoryTable = new BPSheet('buyerCategory');
          $category = $buyerCategoryTable->getListById($categoryId);

          $memberTable = new BPSheet('buyerCategoryMember', $category);
          $member = $memberTable->getNewList();


          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'buyerCategory_member');

          $items = array();
          foreach ($mediaList as $media) {
          if ($media->content != NULL || $media->content != '')
          $member->addItem($media->name, $media->content);
          }
          }

          $updateControl->addRenderView('showAddBuyerCategoryMember', 'buyer_category_member_list', array('categoryId' => $categoryId, 'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('saveBuyerCategory')) {
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');

          if (is_numeric($eventId) && $eventId > 0) {
          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);
          }

          $categoryTable = new BPSheet('buyerCategory', $event);

          if (is_numeric($categoryId) && $categoryId > 0) {
          $categoryList = $categoryTable->getListById($categoryId);
          $categoryList->setOwner($event);
          } else {
          $categoryList = $categoryTable->getNewList();
          }

          //adjust list name
          $categoryListName = 'buyerCategory_' . $categoryList->getId();
          $categoryList->name = $categoryListName;
          $categoryList->save();

          $postData = $processPostObj->parsedModelData();
          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'buyer_info');

          foreach ($mediaList as $media) {
          if ($media instanceof BWMedia) {
          $name = $media->getName();
          $value = $media->getContent();
          $categoryList->addItem($name, $value);
          }
          }

          $updateControl->addRenderView('buyerCategoryList', 'buyer_list', array('eventId' => $eventId));
          }
          if ($senderControl->isAjaxLinkClicked('addBuyerCategory')) {
          $buyerId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('buyerId');
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');


          $buyerTable = new BPSheet('vender');
          $buyer = $buyerTable->getListById($buyerId);

          $buyerCategoryTable = new BPCategorySheet('buyerCategory', $buyer);

          if ($categoryId == NULL || $categoryId == 0) {
          $category = new BPCategory();
          $buyerCategory = $buyerCategoryTable->getNewList();
          }

          if (is_numeric($categoryId) && $categoryId > 0) {
          $category = new BPCategory($categoryId);
          $buyerCategory = $buyerCategoryTable->getList(array('itemData' => array('buyerId' => $buyerId, 'categoryId' => $categoryId)));
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'buyer_category');

          if ($mediaList != NULL) {
          foreach ($mediaList as $media) {
          if ($media->name == 'name') {
          $categoryName = $media->content;
          }
          if ($media->name == 'alias') {
          $categoryAlias = $media->content;
          }
          }

          $category = $category->getRootNode()->addChild(array('name' => $categoryName, 'value' => $categoryAlias));
          $categoryId = $category->getId();
          }

          $buyerCategory->addItem('buyerId', $buyerId);
          $buyerCategory->addItem('categoryId', $categoryId);

          $updateControl->addRenderView('showAddBuyerCategory', 'buyer_add_category', array('buyerId' => $buyerId));
          }
          if ($senderControl->isAjaxLinkClicked('showVenderList')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $updateControl->addRenderView('showVenderList', 'vender_list', array('eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('showAddVender')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');

          $eventTable = new BPEventSheet();
          if (is_numeric($eventId) && $eventId > 0) {
          $event = $eventTable->getListById($eventId);

          if ($venderId == NULL || $venderId == 0) {
          $venderTable = new BPSheet('vender', Blyn::app()->getActiveSpace());
          $venderName = 'vender_' . Blyn::app()->getActiveSpace()->getId() . "_" . Blyn::app()->getCurrentUser()->getId();
          $vender = $venderTable->getListByName($venderName);
          if (!$vender instanceof BTreeList)
          $vender = $venderTable->getNewList($venderName);
          $venderId = $vender->getId();
          }
          }
          $updateControl->addRenderView('showAddVender', 'vender_add', array('venderId' => $venderId, 'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('addVenderAttribute')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $attributeId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('attributeId');


          $venderTable = new BPSheet('vender');
          $vender = $venderTable->getListById($venderId);

          $attributeTable = new BPSheet('venderAttribute', $vender);

          if ($attributeId == NULL || $attributeId == 0) {
          $attribute = $attributeTable->getNewList();
          }

          if (is_numeric($attribute) && $attributeId > 0) {
          $attribute = $attributeTable->getListById($attributeId);
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'vender_attribute');

          $items = array();
          foreach ($mediaList as $media) {
          $attribute->addItem($media->name, $media->content);
          }

          $updateControl->addRenderView('showAddVender', 'vender_add_info', array('venderId' => $venderId, 'eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('deleteVenderAttribute')) {
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $attributeId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('attributeId');


          $venderTable = new BPSheet('vender');
          $vender = $venderTable->getListById($venderId);

          $attributeTable = new BPSheet('venderAttribute', $vender);

          if ($attributeId != NULL && $attributeId != 0) {
          $attribute = $attributeTable->deleteList($attributeId);
          }

          $updateControl->addRenderView('showAddVender', 'vender_add_info', array('venderId' => $venderId, 'eventId' => $venderId));
          }

          if ($senderControl->isAjaxLinkClicked('addVenderCategory')) {
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $categoryId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('categoryId');


          $venderTable = new BPSheet('vender');
          $vender = $venderTable->getListById($venderId);

          $venderCategoryTable = new BPCategorySheet('venderCategory', $vender);

          if ($categoryId == NULL || $categoryId == 0) {
          $category = new BPCategory();
          $venderCategory = $venderCategoryTable->getNewList();
          }

          if (is_numeric($categoryId) && $categoryId > 0) {
          $category = new BPCategory($categoryId);
          $venderCategory = $venderCategoryTable->getList(array('itemData' => array('venderId' => $venderId, 'categoryId' => $categoryId)));
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'vender_category');

          if ($mediaList != NULL) {
          foreach ($mediaList as $media) {
          if ($media->name == 'name') {
          $categoryName = $media->content;
          }
          if ($media->name == 'alias') {
          $categoryAlias = $media->content;
          }
          }

          $category = $category->getRootNode()->addChild(array('name' => $categoryName, 'value' => $categoryAlias));
          $categoryId = $category->getId();
          }

          $venderCategory->addItem('venderId', $venderId);
          $venderCategory->addItem('categoryId', $categoryId);

          $updateControl->addRenderView('showAddVenderCategory', 'vender_add_category', array('venderId' => $venderId));
          }

          if ($senderControl->isAjaxLinkClicked('saveVender')) {
          $venderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('venderId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');

          if (is_numeric($eventId) && $eventId > 0) {
          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);
          }

          $venderTable = new BPSheet('vender', $event);

          if (is_numeric($venderId) && $venderId > 0) {
          $vender = $venderTable->getListById($venderId);
          $vender->setOwner($event);
          } else {
          $vender = $venderTable->getNewList();
          }

          //adjust list name
          $venderName = 'vender_' . $vender->getId();
          $vender->name = $venderName;
          $vender->save();

          $postData = $processPostObj->parsedModelData();
          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'vender_info');

          foreach ($mediaList as $media) {
          if ($media instanceof BWMedia) {
          $name = $media->getName();
          $value = $media->getContent();
          $vender->addItem($name, $value);
          }
          }

          $updateControl->addRenderView('venderList', 'vender_list', array('eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('showOrderConfigList')) {
          //订单配置
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $updateControl->addRenderView('orderConfig_list', 'orderConfig_list', array('eventId' => $eventId));
          }

          if ($senderControl->isAjaxLinkClicked('showAddOrderConfig')) {
          $configId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('configId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');

          $viewData = array();
          $viewData['eventId'] = $eventId;
          $viewData['configId'] = $configId;
          if ($configId == NULL || $configId == 0) {
          $configTable = new BPSheet('orderConfig');
          $listName = 'list_' . Blyn::app()->getActiveSpace()->getId() . '_' . Blyn::app()->getCurrentUser()->getId();
          $list = $configTable->getListByName($listName);
          if (!$list instanceof BTreeList || $list->getId() == NULL)
          $list = $configTable->getNewList(NULL, $listName);
          //$list = $configTable->addList(null, 'temporary_orderConfig');
          $newConfigId = $list->getId();
          $viewData['configId'] = $newConfigId;
          }

          $updateControl->addRenderView('addOrderConfig', 'orderConfig_add', $viewData);
          }

          if ($senderControl->isAjaxLinkClicked('saveOnlineOrderConfig')) {
          $configId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('configId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');

          $eventTable = new BPEventSheet();
          $event = NULL;
          if (is_numeric($eventId) && $eventId > 0) {
          $event = $eventTable->getListById($eventId);
          }

          if ($event != NULL) {
          $configTable = new BPSheet('orderConfig', $event);
          if (is_numeric($configId) && $configId > 0) {
          $config = $configTable->getListById($configId);
          //may not under event when init config
          $config->setOwner($event);
          } else {
          $config = $configTable->getNewList();
          }

          //adjust name
          if ($config instanceof BTreeList) {
          $config->name = 'orderConfig' . $config->getId();
          $config->save();
          }

          //populate medialist for config
          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'onlineOrderConfig_info');

          foreach ($mediaList as $media) {
          $name = $media->name;
          $value = $media->getContent();
          $config->addItem($name, $value);
          }

          //add vender config
          /*
          $venderId = NULL;
          $venderIdItem = $config->getItem('venderId');
          if ($venderIdItem instanceof BTreeItem) {
          $venderId = $venderIdItem->getValue();
          }
          $venderTable = new BPSheet('vender', $config);
          if (is_numeric($venderId) && $venderId > 0) {
          $vender = $venderTable->getListById($venderId);
          } else {
          $vender = $venderTable->getNewList();
          }

          if ($vender instanceof BTreeList) {
          //adjust namee
          $vender->name = 'vender' . $vender->getId();
          $vender->save();

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'vender_info');

          foreach ($mediaList as $media) {
          $vender->addItem($media->getName(), $media->getContent());
          }
          }

          }

          $updateControl->addRenderView('orderConfigList', 'orderConfig_list', array(
          'eventId' => $eventId
          ));
          }

          if ($senderControl->isAjaxLinkClicked('buildOrderData')) {
          $orderConfigId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('orderConfigId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');

          $eventTable = new BPEventSheet();
          $event = $eventTable->getListById($eventId);

          $finalList = array();

          $discountTable = new BPOrderSheet('discount');

          //first get orderConfig
          $orderConfigTable = new BPSheet('orderConfig', $event);
          $orderConfigView = $orderConfigTable->getView();

          foreach ($orderConfigView as $id => $orderConfig) {
          $itemList = array();
          $itemId = 'o' . $id;
          $items = $orderConfig->getItems();
          foreach ($items as $item) {
          if ($item->value != NULL && $item->valueType == 'string') {
          $itemList['profile'] = $item->value;
          }
          }
          $finalList[$itemId] = $itemList;
          }

          //get orderPriceConfig
          $orderPriceConfigTable = new BPSheet('orderPriceConfig', $orderConfig);
          $orderPriceConfigView = $orderPriceConfigTable->getView();

          foreach ($finalList as $fid => $itemList) {
          if ($orderPriceConfigView != NULL || !empty($orderPriceConfigView)) {
          foreach ($orderPriceConfigView as $id => $config) {
          $list = array();
          $itemId = $fid . '_p' . $id;
          //$list['itemId'] = $itemId;
          $list += $itemList;
          $items = $config->getItems();
          foreach ($items as $item) {
          if ($item->value != NULL && $item->valueType == 'string') {
          if (strlen($item->value) < 100) {
          $list[$item->name] = $item->value;
          } else {
          $list['profile'] += " " . $item->value;
          }
          }
          }

          $finalList[$itemId] = $list;
          }
          unset($finalList[$fid]);
          }
          }

          foreach ($orderPriceConfigView as $priceConfig) {

          $priceConfigProductTable = new BPSheet('priceConfigProduct', $priceConfig);
          $priceConfigBuyerTable = new BPSheet('priceConfigBuyer', $priceConfig);
          $priceConfigVenderTable = new BPSheet('priceConfigVender', $priceConfig);
          $priceConfigTimeslotTable = new BPSheet('priceConfigTimeslot', $priceConfig);

          //get priceConfigProduct
          $priceProductView = $priceConfigProductTable->getView();
          $priceBuyerView = $priceConfigBuyerTable->getView();
          $priceVenderView = $priceConfigVenderTable->getView();
          $priceTimeslotView = $priceConfigTimeslotTable->getView();

          foreach ($finalList as $fid => $itemList) {
          foreach ($priceProductView as $id => $config) {
          $list = array();
          $itemId = $fid . '_pp' . $id;
          //$list['itemId'] = $itemId;
          $list += $itemList;
          $items = $config->getItems();
          foreach ($items as $item) {
          if ($item->value != NULL && $item->valueType == 'string') {
          if (strlen($item->value) < 100) {
          $list[$item->name] = $item->value;
          } else {
          $list['profile'] += " " . $item->value;
          }
          }
          }
          $finalList[$itemId] = $list;
          }

          foreach ($priceBuyerView as $id => $config) {
          $list = array();
          $itemId = $fid . '_pb' . $id;
          //$list['itemId'] = $itemId;
          $list += $itemList;
          $items = $config->getItems();
          foreach ($items as $item) {
          if ($item->value != NULL && $item->valueType == 'string') {
          if (strlen($item->value) < 100) {
          $list[$item->name] = $item->value;
          } else {
          $list['profile'] += " " . $item->value;
          }
          }
          }
          $finalList[$itemId] = $list;
          }

          foreach ($priceVenderView as $id => $config) {
          $list = array();
          $itemId = $fid . '_pv' . $id;
          //$list['itemId'] = $itemId;
          $list += $itemList;
          $items = $config->getItems();
          foreach ($items as $item) {
          if ($item->value != NULL && $item->valueType == 'string') {
          if (strlen($item->value) < 100) {
          $list[$item->name] = $item->value;
          } else {
          $list['profile'] += " " . $item->value;
          }
          }
          }
          $finalList[$itemId] = $list;
          }

          foreach ($priceTimeslotView as $id => $config) {
          $list = array();
          $itemId = $fid . '_pt' . $id;
          //$list['itemId'] = $itemId;
          $list += $itemList;
          $items = $config->getItems();
          foreach ($items as $item) {
          if ($item->value != NULL && $item->valueType == 'string') {
          if (strlen($item->value) < 100) {
          $list[$item->name] = $item->value;
          } else {
          $list['profile'] += " " . $item->value;
          }
          }
          }
          $finalList[$itemId] = $list;
          }

          unset($finalList[$fid]);
          }
          }

          foreach ($finalList as $key => $value) {
          $discountTable->addList($value, $key);
          }

          $updateControl->addRenderView('updateOrderData', 'orderConfig_add', array(
          'eventId' => $eventId,
          'configId' => $orderConfigId
          ));
          }

          if ($senderControl->isAjaxLinkClicked('deleteOnlineOrderConfig')) {

          }

          if ($senderControl->isAjaxLinkClicked('showOrderPriceConfigList')) {
          $configId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('configId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $updateControl->addRenderView('productConfig_list', 'orderConfig_add_priceConfig_list', array(
          'eventId' => $eventId,
          'configId' => $configId
          ));
          }

          if ($senderControl->isAjaxLinkClicked('showAddOrderPriceConfig')) {
          $orderPriceConfigId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('orderPriceConfigId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $configId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('configId');

          $priceConfigTable = new BPSheet('orderPriceConfig');
          $orderConfigTable = new BPSheet('orderConfig');
          $orderConfig = $orderConfigTable->getListById($configId);
          if ($orderPriceConfigId == NULL || $orderPriceConfigId == 0) {
          $listId = 'orderPriceConfig_' . Blyn::app()->getActiveSpace()->getId() . '_' . Blyn::app()->getCurrentUser()->getId();
          $priceConfig = $priceConfigTable->getListByName($listId);
          if ($priceConfig == NULL)
          $priceConfig = $priceConfigTable->getNewList(NULL, $listId);
          $orderPriceConfigId = $priceConfig->getId();
          } else {
          $priceConfig = $priceConfigTable->getListById($orderPriceConfigId);
          //$priceConfig->setOwner($orderConfig);
          }

          $viewData = array();
          $viewData['orderPriceConfigId'] = $orderPriceConfigId;
          $viewData['eventId'] = $eventId;
          $viewData['configId'] = $configId;
          $updateControl->addRenderView('addOrderPriceConfig', 'orderConfig_add_priceConfig', $viewData);
          }

          if ($senderControl->isAjaxLinkClicked('saveOrderPriceConfig')) {
          $orderPriceConfigId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('orderPriceConfigId');
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $configId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('configId');

          $orderConfigTable = new BPSheet('orderConfig');
          $orderConfig = $orderConfigTable->getListById($configId);

          $priceConfigTable = new BPSheet('orderPriceConfig', $orderConfig);

          if ($orderPriceConfigId > 0) {
          $priceConfig = $priceConfigTable->getListById($orderPriceConfigId);
          $priceConfig->setOwner($orderConfig);
          //adust name
          $listName = 'orderPrice_' . $orderPriceConfigId;
          $priceConfig->name = $listName;
          $priceConfig->save();
          }

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'priceConfig_info');

          foreach ($mediaList as $media) {
          $field = $media->name;
          $value = $media->content;
          $priceConfig->addItem($field, $value);
          }

          $viewData = array();
          //$viewData['orderPriceConfigId'] = $orderPriceConfigId;
          $viewData['eventId'] = $eventId;
          $viewData['configId'] = $configId;
          $updateControl->addRenderView('showOrderPriceConfigList', 'orderConfig_add_priceConfig_list', $viewData);
          }

          if ($senderControl->isAjaxLinkClicked('addPriceConfigProduct')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $priceConfigId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('orderPriceConfigId');
          $priceProductId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('priceConfigProductId');

          $priceConfigTable = new BPSheet('priceConfig');
          $priceConfig = $priceConfigTable->getListById($priceConfigId);

          $priceProductTable = new BPSheet('priceConfigProduct', $priceConfig);

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'priceConfig_product');

          if ($mediaList != NULL) {

          if ($priceProductId == NULL || $priceProductId == 0) {
          $priceProduct = $priceProductTable->getNewList();
          }

          if (is_numeric($priceProductId) && $priceProductId > 0) {
          $priceProduct = $priceProductTable->getListById($priceProductId);
          }

          $items = array();
          foreach ($mediaList as $media) {
          $value = $media->content;
          if ($value != NULL || $value != '') {
          $priceProduct->addItem($media->name, $media->content);
          }
          }
          }

          $updateControl->addRenderView('showPriceConfigProduct', 'orderConfig_add_priceConfig_product', array('eventId' => $eventId, 'orderPriceConfigId' => $priceConfigId));
          }

          if ($senderControl->isAjaxLinkClicked('addPriceConfigBuyer')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $priceConfigId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('orderPriceConfigId');
          $priceBuyerId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('priceConfigBuyerId');

          $priceConfigTable = new BPSheet('priceConfig');
          $priceConfig = $priceConfigTable->getListById($priceConfigId);

          $priceBuyerTable = new BPSheet('priceConfigBuyer', $priceConfig);

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'priceConfig_buyer');

          if ($mediaList != NULL) {

          if ($priceBuyerId == NULL || $priceBuyerId == 0) {
          $priceBuyer = $priceBuyerTable->getNewList();
          }

          if (is_numeric($priceBuyerId) && $priceBuyerId > 0) {
          $priceBuyer = $priceBuyerTable->getListById($priceBuyerId);
          }

          $items = array();
          foreach ($mediaList as $media) {
          $value = $media->content;
          if ($value != NULL || $value != '') {
          $priceBuyer->addItem($media->name, $media->content);
          }
          }
          }

          $updateControl->addRenderView('showPriceConfigBuyer', 'orderConfig_add_priceConfig_buyer', array('eventId' => $eventId, 'orderPriceConfigId' => $priceConfigId));
          }

          if ($senderControl->isAjaxLinkClicked('addPriceConfigVender')) {
          $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
          $priceConfigId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('orderPriceConfigId');
          $priceVenderId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('priceConfigVenderId');

          $priceConfigTable = new BPSheet('priceConfig');
          $priceConfig = $priceConfigTable->getListById($priceConfigId);

          $priceVenderTable = new BPSheet('priceConfigVender', $priceConfig);

          $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'priceConfig_vender');

          if ($mediaList != NULL) {

          if ($priceVenderId == NULL || $priceVenderId == 0) {
          $priceVender = $priceVenderTable->getNewList();
          }

          if (is_numeric($priceVenderId) && $priceVenderId > 0) {
          $priceVender = $priceVenderTable->getListById($priceVenderId);
          }

          $items = array();
          foreach ($mediaList as $media) {
          $value = $media->content;
          if ($value != NULL || $value != '') {
          $priceVender->addItem($media->name, $media->content);
          }
          }
          }

          $updateControl->addRenderView('showPriceConfigVender', 'orderConfig_add_priceConfig_vender', array('eventId' => $eventId, 'orderPriceConfigId' => $priceConfigId));
          }

          if ($senderControl->isAjaxLinkClicked('manageOrder')) {
          //订单管理
          $updateControl->addRenderView('order_add', 'order_add');
          }
         * 
         */
    }

    public function runClient($data = NULL) {

        /*
        $senderControl = Blyn::app()->getUIManager()->getSenderControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();

        if ($senderControl instanceof BUIOperation) {
            $eventCooker = new BAEventCooker();
            $eventCooker->showEventListByOperation($screen);
        }
         * 
         */

        /*
          if ($senderControl->isAjaxLinkClicked('showOrderList')) {
          $updateControl->addRenderView('client_orderList', 'client_orderList');
          }
          if ($senderControl->isAjaxLinkClicked('showGoodsDetail')) {
          $updateControl->addRenderView('client_goodsDetail', 'client_goodsDetail');
          }
          if ($senderControl->isAjaxLinkClicked('addOrder')) {

          }

          if ($senderControl->isAjaxLinkClicked('deleteOrder')) {

          }

          if ($senderControl->isAjaxLinkClicked('showShoppingCartList')) {

          }

          if ($senderControl->isAjaxLinkClicked('manageShoppingCart')) {

          }

          if ($senderControl->isAjaxLinkClicked('findAvailableOrderList')) {

          }
         * 
         */
    }

    public function runManagement($data = NULL) {
        /*
          $senderControl = Blyn::app()->getUIManager()->getSenderControl();
          $screen = Blyn::app()->getUIManager()->getCurrentScreen();

          if ($senderControl instanceof BUIOperation) {
          $eventCooker = new BAEventCooker();
          $eventCooker->showEventListByOperation($screen);
          }
         * 
         */
    }

}
